package com.yao.longhan.ui.base;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;

import com.yao.longhan.AppManager;
import com.yao.longhan.BaseApplication;
import com.yao.longhan.R;
import com.yao.longhan.eventbus.FinishEvent;
import com.yao.longhan.eventbus.RxBus;
import com.yao.longhan.eventbus.RxEvent;
import com.yao.longhan.net.BaseCallback;
import com.yao.longhan.net.RetrofitService;
import com.yao.longhan.ui.widget.view.CustomDialog;
import com.yao.longhan.utils.StatusBarCompat;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.yao.longhan.AppManager.getAppManager;

/**
 * Created by heyao on 2017/7/17.
 */

public abstract class BaseActivity extends AppCompatActivity implements BaseCallback.CallBackListener {
    protected String TAG = this.getClass().getSimpleName();
    protected BaseApplication mApplication;
    protected RetrofitService mRetrofitService;
    private Unbinder bind;
    private Disposable mDisposable;

    protected int statusBarColor = 0;
    protected View statusBarView = null;
    private CustomDialog dialog;//进度条

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBefore();
        setContentView(inflateLayout());
        if (statusBarColor == 0) {
            statusBarView = StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.colorPrimaryDark));
        } else if (statusBarColor != -1) {
            statusBarView = StatusBarCompat.compat(this, statusBarColor);
        }

        bind = ButterKnife.bind(this);
        mApplication = (BaseApplication) getApplication();
        mRetrofitService = mApplication.getRetrofitService();
        getAppManager().addActivity(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        }
        initView(savedInstanceState);
        initAfter();
        registerRxBus();
    }

    protected void initBefore() {

    }

    protected abstract int inflateLayout();

    protected void initView(Bundle savedInstanceState) {
        if (!TextUtils.equals(TAG, "MainActivity")) {
            Toolbar toolBar = ButterKnife.findById(this, R.id.tool_bar);
            if (toolBar != null) {
                setSupportActionBar(toolBar);
                ActionBar actionBar = getSupportActionBar();
                if (actionBar != null) {
                    actionBar.setDisplayHomeAsUpEnabled(true);
                    actionBar.setDisplayShowTitleEnabled(false);
                }
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                AppManager.getAppManager().finishActivity();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    protected abstract void initAfter();

    protected <T> void toActivityForResult(Class<T> clazz, Bundle bundle, int requestCode) {
        Intent intent = new Intent(this, clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    protected <T> void toActivity(Class<T> clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    private void registerRxBus() {
        mDisposable = RxBus.getDefault().toObservable(RxEvent.class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(rxEvent -> {
                    if (rxEvent != null) {
                        if (rxEvent instanceof FinishEvent) {
                            finish();
                        } else {
                            onEventMainThread(rxEvent);
                        }
                    }
                });
    }

    protected void onEventMainThread(RxEvent rxEvent) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
        dismissDialog();
        if (!mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }

    @Override
    public void onSuccess(int requestCode, Object object) {
    }

    @Override
    public void onFailed(int requestCode, String message) {
    }

    protected void hideStatusBar() {
        WindowManager.LayoutParams attrs = getWindow().getAttributes();
        attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(attrs);
        if (statusBarView != null) {
            statusBarView.setBackgroundColor(Color.TRANSPARENT);
        }
    }

    protected void showStatusBar() {
        WindowManager.LayoutParams attrs = getWindow().getAttributes();
        attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(attrs);
        if (statusBarView != null) {
            statusBarView.setBackgroundColor(statusBarColor);
        }
    }

    protected boolean isVisible(View view) {
        return view.getVisibility() == View.VISIBLE;
    }

    // dialog
    public CustomDialog getDialog() {
        if (dialog == null) {
            dialog = CustomDialog.instance(this);
            dialog.setCancelable(true);
        }
        return dialog;
    }

    public void hideDialog() {
        if (dialog != null)
            dialog.hide();
    }

    public void showDialog() {
        getDialog().show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }
}
